<template>
	<el-container>
        <el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			 
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="角色名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
        <el-main class="nopadding" style="padding: 10px;">
            
            <table class="table" cellpadding="0" :cellspacing="0">
                 <tr style="height:30px;">
					<th width="50"><el-checkbox v-model="chkall"  size="small" @change="xuan_all"/></th><th>ID</th><th class="left">名称</th><th>图标</th><th>路径</th><th>别名</th><th>组件</th><th>类型</th><th>排序</th><th>状态</th><th>操作</th>
                 </tr>
               
                <tr v-for="(item,index) in menuList" :key="item.value" v-show='item.level<=2' class="tatr">
					<td>
						<el-checkbox v-model="item.id"  size="small" @change="selectionChange" :checked="true" v-if="item.chk==true"/>
						<el-checkbox v-model="item.id"  size="small" @change="selectionChange" v-if="item.chk==false || item.chk==null"/>
					</td>
					<td>{{ item.id }}</td>
                    <td class="left">
                            <span :class="'padding'+item.level" v-if="item.level > 0">﹂</span>
                             <span v-if="item.level == 0" style="color:#333;font-weight: bold;">{{item.title }}</span>
                             <span v-if="item.level > 0">{{item.title }}</span>
                    </td>
                    <td><el-icon><component :is="item.icon" /></el-icon></td>
                    <td>{{ item.path }}</td>
                    <td>{{ item.name }}</td>
					<td>{{ item.component }}</td>
                    <td>{{ item.type }}</td>
                    <td>{{item.sort}}</td>
                    <td>
                        <el-tag v-if="item.status==1" type="success" size="small">启用</el-tag>
                        <el-tag v-if="item.status==0" type="danger" size="small">停用</el-tag>
                    </td>
                    <td>
                    <el-popconfirm title="确定删除吗？" @confirm="table_del(item.id)">
                        <template #reference>
                            <el-button text type="primary" size="small">删除</el-button>
                        </template>
                    </el-popconfirm>
                    <el-button text type="primary" size="small" @click="table_edit(index)">编辑</el-button>
                    </td>
                </tr>
                     
            </table>

        </el-main>
		 
	</el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	 
     import saveDialog from './save'

	export default {
		name: "settingMenu",
		components: {
			saveDialog,
		},
		data(){
			return {
				chkall:false,
                dialog: {
					save: false,
					permission: false
				},
				menuList:[],
				selection: [],
				search: {
					keyword: null
				},
			}
		},
        mounted() {
			this.getMenu();
		},
		methods: {
            //加载树数据
			async getMenu(){
				var res = await this.$API.system.universal.list.get({},'/system/menu2');
				this.menuloading = false
				this.menuList = res.data;
				
			},
			xuan_all(){
				  
					var arr = this.menuList
					for(var i= 0;i<arr.length;i++){
							arr[i].chk = this.chkall
					}
					//this.menuList = arr
					console.log(this.menuList,"xxxxxxxxx")
	 
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(index){
				this.dialog.save = true
                var row = this.menuList[index]
				this.$nextTick(() => {
                    row.status = row.status.toString() //此处得吧数字类型转为字符 否则switch有问题
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row,'act':'del'}
				var res = await this.$API.system.universal.handle.post(reqData,'/system/post_menu');
				if(res.code == 200){
					this.getMenu();
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.$refs.table.refresh()
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
				console.log(this.selection,"sssssssssssss")
			},
			//表格内开关
			changeSwitch(val, row){
				row.status = row.status == '1'?'0':'1'
				row.$switch_status = true;
				setTimeout(()=>{
					delete row.$switch_status;
					row.status = val;
					this.$message.success("操作成功")
				}, 500)
			},
			//搜索
			upsearch(){

			},
			//根据ID获取树结构
			filterTree(id){
				var target = null;
				function filter(tree){
					tree.forEach(item => {
						if(item.id == id){
							target = item
						}
						if(item.children){
							filter(item.children)
						}
					})
				}
				filter(this.$refs.table.tableData)
				return target
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					// this.$refs.table.refresh()
                    this.getMenu();
				}else if(mode=='edit'){
                    this.getMenu();
				}
			}
		}
	}
</script>

<style scoped>
.padding2{
	padding-left: 50px
}
.table{
    width:100%;
    border-collapse: collapse;
    .left{
      text-align: left;
    }
    th{
        color:#888
    }
    .padding1{
        padding-left:25px;
    }
    .padding2{
        padding-left: 60px;
    }
}
.tatr:hover{
	background-color:#eee;
}
.table th,.table td{
    border:1px solid #e0e0e0;
    padding:3px 15px;
    text-align: center;
    color:#333;
}
	.menu:deep(.el-tree-node__label) {display: flex;flex: 1;height:100%;}
	.custom-tree-node {display: flex;flex: 1;align-items: center;justify-content: space-between;font-size: 14px;height:100%;padding-right:24px;}
	.custom-tree-node .label {display: flex;align-items: center;;height: 100%;}
	.custom-tree-node .label .el-tag {margin-left: 5px;}
	.custom-tree-node .do {display: none;}
	.custom-tree-node .do i {margin-left:5px;color: #999;}
	.custom-tree-node .do i:hover {color: #333;}

	.custom-tree-node:hover .do {display: inline-block;}
</style>
